/**
 * User: zsquirrel
 * Date: 2020/4/30
 * Time: 9:48 上午
 */
package com.cskaoyan.mall.service;

import com.cskaoyan.mall.dao.GoodsDao;
import com.cskaoyan.mall.dao.GoodsDaoImpl;
import com.cskaoyan.mall.dao.UserDao;
import com.cskaoyan.mall.dao.UserDaoImpl;
import com.cskaoyan.mall.model.*;
import com.cskaoyan.mall.model.bo.AskGoodsMsgBO;
import com.cskaoyan.mall.model.bo.GoodsParamBO;
import com.cskaoyan.mall.model.bo.ReplyBO;
import com.cskaoyan.mall.model.bo.SpecBO;
import com.cskaoyan.mall.model.enmu.MsgState;
import com.cskaoyan.mall.model.vo.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsServiceImpl implements GoodsService {

    private GoodsDao goodsDao = new GoodsDaoImpl();

    private UserDao userDao = new UserDaoImpl();

    @Override
    public List<Type> getType() {
        return goodsDao.getType();
    }

    @Override
    public List<Goods> getGoodsByType(String typeId) {
        return goodsDao.getGoodsByType(typeId);
    }

    /**
     * 将参数里面的数据填充到两张表中
     * service层代码应该怎么写
     * 是直接调用dao还是service需要做逻辑
     * @param goodsParamBO
     */
    @Override
    public void addGoods(GoodsParamBO goodsParamBO) {
        List<Spec> specList = goodsParamBO.getSpecList();
        //规格中的最小值
        double price = specList.get(0).getUnitPrice();
        //取最大值
        int stockNum = specList.get(0).getStockNum();
        for (int i = 1; i < specList.size(); i++) {
            if(price > specList.get(i).getUnitPrice()){
                price = specList.get(i).getUnitPrice();
            }
            if(stockNum < specList.get(i).getStockNum()){
                stockNum = specList.get(i).getStockNum();
            }
        }
        goodsParamBO.setPrice(price);
        goodsParamBO.setStockNum(stockNum);
        int goodsId = goodsDao.addGoods(goodsParamBO);
        //spec表和goods表怎么关联？
        goodsDao.addSpecList(goodsId, specList);
    }

    /**
     * 删除type
     * @param typeId
     */
    @Override
    public void deleteType(String typeId) {
        goodsDao.deleteType(typeId);
    }

    /**
     * 查询出商品信息以及当前商品的spec信息
     * @param id
     * @return
     */
    @Override
    public GoodsVO getGoodsInfo(String id) {
        List<Spec> specs = goodsDao.getSpecs(id);
        Goods goods = goodsDao.getGoodsById(id);
        GoodsVO goodsVO = new GoodsVO();
        goodsVO.setGoods(goods);
        goodsVO.setSpecs(specs);
        return goodsVO;
    }

    @Override
    public void deleteSpec(SpecBO specBO) {
        goodsDao.deleteSpec(specBO);
    }

    @Override
    public void addSpec(Spec spec) {
        goodsDao.addSpec(spec);
    }

    /**
     * 业务逻辑稍微有些复杂，需要修改商品表以及规格表
     * 其次规格表还可能有新增的规格
     * 所以通过规格的id来判断是否是新增规格
     * 有id即为原先规格；没有id即为新增规格
     * @param paramBO
     */
    @Override
    public void updateGoods(GoodsParamBO paramBO) {
        goodsDao.updateGoods(paramBO);
        List<Spec> specList = paramBO.getSpecList();
        List<Spec> updateSpecs = new ArrayList<>();
        List<Spec> addSpecs = new ArrayList<>();
        double price = specList.get(0).getUnitPrice();
        int stockNum = specList.get(0).getStockNum();
        for (Spec spec : specList) {
            if(spec.getUnitPrice() < price){
                price = spec.getUnitPrice();
            }
            if(spec.getStockNum() > stockNum){
                stockNum = spec.getStockNum();
            }
            if(spec.getId() != null){
                updateSpecs.add(spec);
            }else{
                addSpecs.add(spec);
            }
        }
        paramBO.setPrice(price);
        paramBO.setStockNum(stockNum);
        goodsDao.addSpecList(paramBO.getId(),addSpecs);
        goodsDao.updateSpecList(updateSpecs);
    }

    /**
     * 首先删除商品的规格，接着商品信息
     * 可以逻辑删除，也可以物理删除
     * 可以加入事务
     * @param id
     */
    @Override
    public void deleteGoods(String id) {
        goodsDao.deleteSpec(id);
        goodsDao.deleteGoods(id);
    }

    @Override
    public List<NoReplyMsgVO> noReplyMsg() {
        List<MessageVO> msgList = goodsDao.getMsg(MsgState.NO_REPLY.getCode());
        List<NoReplyMsgVO> noReplyMsgVOList = new ArrayList<>();
        for (MessageVO messageVO : msgList) {
            NoReplyMsgVO noReplyMsgVO = new NoReplyMsgVO();
            noReplyMsgVO.setId(messageVO.getId());
            noReplyMsgVO.setUserId(messageVO.getUserId());
            noReplyMsgVO.setContent(messageVO.getContent());
            noReplyMsgVO.setState(messageVO.getState());
            noReplyMsgVO.setCreatetime(messageVO.getCreatetime());
            Map goods = new HashMap();
            goods.put("name", messageVO.getGoodsName());
            Map user = new HashMap();
            user.put("name", messageVO.getUserName());
            noReplyMsgVO.setGoods(goods);
            noReplyMsgVO.setUser(user);
            noReplyMsgVOList.add(noReplyMsgVO);
        }
        return noReplyMsgVOList;
    }

    @Override
    public void reply(ReplyBO replyBO) {
        goodsDao.reply(replyBO);
    }

    @Override
    public List<RepliedMsgVO> repliedMsg() {
        List<MessageVO> msgList = goodsDao.getMsg(MsgState.REPLIED.getCode());
        List<RepliedMsgVO> repliedMsgVOS = new ArrayList<>();
        for (MessageVO messageVO : msgList) {
            RepliedMsgVO repliedMsgVO = new RepliedMsgVO();
            repliedMsgVO.setId(messageVO.getId());
            repliedMsgVO.setUserId(messageVO.getUserId());
            repliedMsgVO.setContent(messageVO.getContent());
            repliedMsgVO.setReplyContent(messageVO.getReplyContent());
            repliedMsgVO.setState(messageVO.getState());
            repliedMsgVO.setCreatetime(messageVO.getCreatetime());
            Map goods = new HashMap();
            goods.put("name", messageVO.getGoodsName());
            Map user = new HashMap();
            user.put("name", messageVO.getUserName());
            repliedMsgVO.setGoods(goods);
            repliedMsgVO.setUser(user);
            repliedMsgVOS.add(repliedMsgVO);
        }
        return repliedMsgVOS;

    }

    @Override
    public List<Goods> searchGoods(String keyword) {
        return goodsDao.searchGoods(keyword);
    }

    /**
     * 用于显示商品详情页商品信息
     * @param id
     * @return
     */
    @Override
    public GoodsInfoVO goodsInfo(String id) {
        List<Spec> specs = goodsDao.getSpecs(id);
        Goods goods = goodsDao.getGoodsById(id);
        GoodsInfoVO goodsInfoVO = new GoodsInfoVO();
        goodsInfoVO.setImg(goods.getImg());
        goodsInfoVO.setName(goods.getName());
        goodsInfoVO.setDesc(goods.getDesc());
        goodsInfoVO.setTypeId(goods.getTypeId());
        goodsInfoVO.setSpecs(specs);
        return goodsInfoVO;
    }

    @Override
    public List<GoodsInfoMsgVO> getAllMsgs(String goodsId) {
        List<GoodsInfoMsgVO> allMsgs = goodsDao.getAllMsgs(goodsId);
        return allMsgs;
    }

    @Override
    public GoodsCommentListVO getGoodsComment(String goodsId) {
        List<GoodsCommentVO> goodsComments = goodsDao.getGoodsComment(goodsId);
        GoodsCommentListVO goodsCommentListVO = new GoodsCommentListVO();
        Integer totalCount = goodsDao.getTotalCommentsCount(goodsId);
        if(totalCount != 0){
            Integer positiveCount = goodsDao.getPositiveCommentCount(goodsId);
            goodsCommentListVO.setCommentList(goodsComments);
            goodsCommentListVO.setRate(positiveCount * 100.0 / totalCount);
        }else {
            goodsCommentListVO.setRate(0.0);
        }
        return goodsCommentListVO;
    }

    @Override
    public void askGoodsMsg(AskGoodsMsgBO askGoodsMsgBO) {
        //拿到userId
        List<User> users = userDao.searchUser(askGoodsMsgBO.getToken());
        Integer userId = users.get(0).getId();
        goodsDao.askGoodsMsg(userId, askGoodsMsgBO.getMsg(), askGoodsMsgBO.getGoodsId());
    }
}
